package com.ezandroid.library.network.http.parser;

import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 抽象的输入流解析器
 *
 * @author markmjw
 * @date 2014-06-13
 */
public abstract class BaseStreamParser<T> implements IParser<T> {

    @Override
    public T parse(byte[] data) {
        InputStream inputStream = null;
        try {
            inputStream = new ByteArrayInputStream(data);
            return parseStream(inputStream);
        } catch (IOException e) {
            Log.e("Parse error.", Log.getStackTraceString(e));
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 解析输入流
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    protected abstract T parseStream(InputStream inputStream) throws IOException;
}
